#include "seqListIndex.h"
#include <stdlib.h>
// init
LT *initList()
{
    LT *header = (LT *)malloc(sizeof(LT));
    ERROE_CHECK(NULL, header, "malloc");
    header->count = -1;
    return header;
}
// add

void addlistVal(LT *header, int val)
{
    if (header->count < LEN)
    {
        header->data[++header->count] = val;
    }
    else
    {
        puts("list is full");
    }
}
// del
int delListVal(LT *header, int index)
{
    if (index <= header->count)
    {
        for (size_t i = index; i < header->count; i++)
        {
            header->data[index] = header->data[index + 1];
        }
        header->count--;
    }
    else
    {
        puts("illegal index");
        exit(1);
    }
}
int selectListVal(LT *header, int index)
{
    if (index <= header->count)
    {
        return header->data[index];
    }
    else
    {
        puts("illegal index");
        exit(1);
    }
}
void changeListVal(LT *header, int index, int val)
{
    if (index <= header->count)
    {
        header->data[index] = val;
    }
    else
    {
        puts("illegal index");
    }
}
void showListVal(LT *header)
{
    if (isEmpty(header))
    {
        puts("empty list!");
        exit(1);
    }
    else
    {
        for (size_t i = 0; i <= header->count; i++)
        {
            printf("%d ", header->data[i]);
        }
        puts("");
    }
}
void freeList(LT **header)
{
    free(*header);
    *header = NULL;
}
bool isEmpty(LT *header)
{
    return -1 == header->count ? true : false;
}
void insertValIndex(LT *header, int index, int val)
{
    if (header->count < LEN - 1)
    {
        if (index <= header->count + 1)
        {
            for (size_t i = header->count + 1; i > index; i--)
            {
                header->data[i] = header->data[i-1];
            }
            header->data[index] = val;
            header->count++;
        }else{
            puts("illegal index");
        }
        
    }
    else
    {
        puts("list full");
        exit(1);
    }
}
